package xy.rabbitmq.conf;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TopicRabbitConfig {

    /**
     * 绑定键
     */
    public final static String MAN_QUEUE = "topic.man";
    public final static String WO_MAN_QUEUE = "topic.woman";


    /**
     * 交换机名称
     */
    public static final String TOPIC_Exchange = "TOPIC_Exchange";

    @Bean
    public Queue manQueue() {
        return new Queue(MAN_QUEUE);
    }

    @Bean
    public Queue woManQueue() {
        return new Queue(WO_MAN_QUEUE);
    }

    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange(TOPIC_Exchange);
    }

    /**
     * manQueue和topicExchange绑定 绑定的键值为topic.man
     * 这样只要是消息携带的路由键是topic.man,才会分发到该队列
     * @return
     */
    @Bean
    Binding bindingExchangeMessage() {
        return BindingBuilder.bind(manQueue()).to(topicExchange()).with(MAN_QUEUE);
    }

    /**
     * woman和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
     *这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
     * @return
     */
    @Bean
    Binding bindingExchangeMessage2() {
        return BindingBuilder.bind(woManQueue()).to(topicExchange()).with("topic.#");
    }

}
